package com.mitcub.panda.utils;


import java.io.Serializable;

/**
 * @项目名称: Search
 * @类名称: RespResult
 * @类描述: Restful统一Json响应对象封装
 * 返回泛型效率要高于Object,Object需要强制类型转换
 * 实现了Serializable接口 通过流bytes传输方式web传输，速率更块
 * @作者: tangloong
 * @创建时间: 2021/6/28 11:52
 */
public class RespResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    //返回状态码
    private long code;
    //返回消息
    private String msg;
    //返回内容
    private T data;
    //分页信息
    private Long count;

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public RespResult(){
        this.code = 0;
        this.msg = "success";
    }

    public RespResult(long code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public RespResult(long code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public RespResult(long code, String msg, T data, Long count) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.count = count;
    }


    //快速返回成功
    public static <T> RespResult success(){
        return new RespResult<T>(0,"success");
    }

    public static <T> RespResult success(long code){
        return new RespResult<T>(code,"success");
    }

    public static <T> RespResult success(String msg){
        return new RespResult<T>(0,msg);
    }

    public static <T> RespResult success(T data){
        return new RespResult<T>(0,"success",data);
    }

    public static <T> RespResult success(String msg, T data){
        return new RespResult<T>(0,msg,data);
    }

    public static <T> RespResult success(T data, Long count){
        return new RespResult<T>(0,"success",data, count);
    }

    public static <T> RespResult success(String msg, T data, Long count){
        return new RespResult<T>(0,msg,data, count);
    }

    //快速返回失败状态
    public static <T> RespResult error(){
        return new RespResult<T>(1, "error");
    }
    public static <T> RespResult error(String msg){
        return new RespResult<T>(1,msg);
    }
    public static <T> RespResult error(long code, String msg){
        return new RespResult<T>(code,msg);
    }

}
